package ai.people.core.nebula.entity;

import ai.people.core.nebula.util.NebulaQueryUtils;
import com.vesoft.nebula.client.graph.data.Node;
import com.vesoft.nebula.client.graph.data.ValueWrapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;

import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * 说明
 *
 * @author zhenglin
 * @since 2022/3/30
 */
@Data
public class NebulaGraph {

    /**
     * 根id
     */
    private NebulaNode rootNode;

    /**
     * 节点
     */
    private Set<String> nodesVidSet;

    /**
     * 节点集
     */
    private Set<NebulaNode> nodeSet;

    /**
     * 关系
     */
    private Set<NebulaPath> paths;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class NebulaPath {
        /**
         * from
         */
        private NebulaNode from;
        /**
         * to
         */
        private NebulaNode to;
        /**
         * 边
         */
        private NebulaNode edge;

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            NebulaPath nebulaPath = (NebulaPath) o;
            return Objects.equals(from, nebulaPath.from) && Objects.equals(to, nebulaPath.to) && Objects.equals(edge, nebulaPath.edge);
        }

        @Override
        public int hashCode() {
            return Objects.hash(from, to, edge);
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class NebulaNode {
        private Object vid;
        private String tagCode;
        private Map<String, Object> properties;

        public NebulaNode(Node node) throws UnsupportedEncodingException {
            this.vid = NebulaQueryUtils.getValue(node.getId());
            this.properties = NebulaQueryUtils.getNodeProperties(node);
            this.tagCode = CollectionUtils.isNotEmpty(node.tagNames()) ? node.tagNames().get(0) : null;
        }

        public NebulaNode(String vid, HashMap<String, ValueWrapper> properties) throws UnsupportedEncodingException {
            this.vid = vid;
            this.properties = NebulaQueryUtils.getProperties(properties);
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            NebulaNode that = (NebulaNode) o;
            return Objects.equals(vid, that.vid) && Objects.equals(tagCode, that.tagCode) && Objects.equals(properties, that.properties);
        }

        @Override
        public int hashCode() {
            return Objects.hash(vid, tagCode, properties);
        }
    }
}
